package com.zyk.hetong.controller;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReferenceUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bw.itext.pdf.service.PdfService;
import com.bw.zhanggm.common.AjaxResult;
import com.zyk.hetong.entity.Contract;
import com.zyk.hetong.service.IContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 合同信息表 前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2024-03-27
 */
@RestController
@RequestMapping("/hetong")
public class ContractController {

    @Autowired
    private IContractService iContractService;

    @Autowired
    PdfService pdfService;

    @Autowired
    HttpServletResponse response;


    @GetMapping("page")
    public AjaxResult page(Contract contract, Page page) {
        //后端校验
        if (StrUtil.isBlank(contract.getEmail())) {
            return AjaxResult.error("邮箱不能为空");
        }
        //查询条件
        QueryWrapper<Contract> queryWrapper = new QueryWrapper<>();//硬编码
        queryWrapper.eq("email", contract.getEmail());

        LambdaQueryWrapper<Contract> query = new LambdaQueryWrapper<>();
//        query.eq(StrUtil.isNotBlank(contract.getEmail()), Contract::getEmail, contract.getEmail());

        Page pageInfo = iContractService.page(page, queryWrapper);
        return AjaxResult.success(pageInfo);
    }


    @GetMapping("export")
    public void export() throws Exception {
        List<Contract> list = iContractService.list();
        String fileName = UUID.randomUUID().toString() + ".pdf";
        //生成pdf
        pdfService.export(list, fileName, "合同列表", Contract.class);
        pdfService.sign(fileName,"hetong"+fileName);
        pdfService.download("hetong"+fileName, response);
    }

    @GetMapping("exportById")
    public void exportById(String id) throws Exception {
        Contract contract = iContractService.getById(id);
        String fileName = UUID.randomUUID().toString() + ".pdf";
        //生成pdf
//        pdfService.export(Arrays.asList(contract), fileName, "合同详情", Contract.class);
        pdfService.exportSinge(contract, fileName, "hetong", Contract.class);
        pdfService.sign(fileName,"hetong"+fileName);
        pdfService.download(fileName, response);
    }

    @GetMapping("preview")
    public void preview() throws Exception {
        List<Contract> list = iContractService.list();
        String fileName = UUID.randomUUID().toString() + ".pdf";
        //生成pdf
        pdfService.export(list, fileName, "合同列表", Contract.class);
        pdfService.sign(fileName,"hetong"+fileName);
        pdfService.preview("hetong"+fileName, response);
    }

}
